/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.ac.iie.di.ban.news.server.util.tracer;

import cn.ac.iie.di.ban.news.server.util.SenderPool;
import cn.ac.iie.di.datadock.rdata.exchange.client.core.REFieldType;
import cn.ac.iie.di.datadock.rdata.exchange.client.exception.REConnectionException;
import cn.ac.iie.di.datadock.rdata.exchange.client.exception.RESessionException;
import cn.ac.iie.di.datadock.rdata.exchange.client.v1.session.RESendSession;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import java.util.HashMap;

/**
 *
 * @author zwlin
 */
public class RmqDataTracer implements DataTracer {

	@Override
	public String getTracerDetail() {
		return "rmq tracer";
	}

	private static class Rec {

		String _id;
		int msg_type;
		ERR_TYPE err_type;
		String content;
		long get_time;
		int total_count;
		int err_count;

		public Rec(String _id, int msg_type, ERR_TYPE err_type, String content, long get_time, int total_count, int err_count) {
			this._id = _id;
			this.msg_type = msg_type;
			this.err_type = err_type;
			this.content = content;
			this.get_time = get_time;
			this.total_count = total_count;
			this.err_count = err_count;
		}

		public Rec() {
		}

	}

	SenderPool pool;

	String scope;

	public RmqDataTracer(String namesrv, String topic, String dataServerType) throws REConnectionException {
		HashMap<String, REFieldType> fields = new HashMap<>();
		fields.put(SCOPE_FIELD_NAME, REFieldType.String);
		fields.put(ID_FIELD_NAME, REFieldType.String);
		fields.put(MSG_TYPE_FIELD_NAME, REFieldType.Int);
		fields.put(ERR_TYPE_FIELD_NAME, REFieldType.Int);
		fields.put(CONTENT_FIELD_NAME, REFieldType.String);
		fields.put(GET_TIME_FIELD_NAME, REFieldType.Long);
		fields.put(ERR_COUNT_FIELD_NAME, REFieldType.Int);
		fields.put(TOTAL_COUNT_FIELD_NAME, REFieldType.Int);
		pool = new SenderPool(namesrv, topic, fields);
		scope = dataServerType;
	}

	@Override
	public void traceJsonParseError(String key, String content, long recvTimeSec) throws Exception {
		trace(new Rec(key, 0, ERR_TYPE.JSON_ERROR, content, recvTimeSec, 1, 1));
	}

	@Override
	public void traceMD5Error(String key, String content, long recvTimeSec) throws Exception {
		trace(new Rec(key, 0, ERR_TYPE.MD5_ERROR, content, recvTimeSec, 1, 1));
	}

	@Override
	public void traceCorrect(String key, JSONObject srcPac, JSON trackRecList, int totalCount, long recvTimeSec) throws Exception {
		trace(new Rec(key, srcPac.getInteger("msg_type"), ERR_TYPE.NO_ERROR, trackRecList.toJSONString(), recvTimeSec, totalCount, 0));
	}

	@Override
	public void traceJsonCheckError(String key, JSONObject srcPac, JSON trackRecList, int totalCount, int errorCount, long recvTimeSec) throws Exception {
		trace(new Rec(key, srcPac.getInteger("msg_type"), ERR_TYPE.CHECK_ERROR, trackRecList.toJSONString(), recvTimeSec, totalCount, errorCount));
	}

	private void trace(Rec rec) throws RESessionException, REConnectionException {
		RESendSession sender = pool.Borrow();
		try {
			sender.setString(ID_FIELD_NAME, rec._id);
			sender.setInt(MSG_TYPE_FIELD_NAME, rec.msg_type);
			sender.setInt(ERR_TYPE_FIELD_NAME, rec.err_type.getType());
			sender.setString(CONTENT_FIELD_NAME, rec.content);
			sender.setLong(GET_TIME_FIELD_NAME, rec.get_time);
			sender.setInt(ERR_COUNT_FIELD_NAME, rec.err_count);
			sender.setInt(TOTAL_COUNT_FIELD_NAME, rec.total_count);
			try {
				sender.add();
			} catch (Exception e) {
				while (true) {
					try {
						sender.flush();
						break;
					} catch (Exception ee) {
					}
				}
				sender.add();
			}
			while (true) {
				try {
					sender.flush();
					break;
				} catch (Exception ee) {
				}
			}
		} finally {
			pool.Return(sender);
		}
	}
}
